﻿Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Partial Class nFile
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If Not Page.IsPostBack Then

        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If Not Page.IsValid Then Exit Sub
        On Error GoTo hErr
        'Open a file for reading
        Dim strName, strCode As String
        Dim iIndex, iCount As Int16
        iCount = 0
        iIndex = CInt(txtCol.Text) - 1
        If iIndex < 1 Then iIndex = 1
        strName = txtName.Text
        strCode = txtCode.Text
        Dim FILENAME As String = Server.MapPath("upload\" & strName.ToString)
        'Get a StreamReader class that can be used to read the file
        Dim objStreamReader As StreamReader
        objStreamReader = File.OpenText(FILENAME)

        'Now, read the entire file into a string
        'Dim contents As String = objStreamReader.ReadToEnd()
        Dim sString As String
        Dim strDataFields() As String
        Dim i, intMin As Integer
        i = 0
        Dim sSql As String
        Dim myConn As New SqlConnection(ConfigurationManager.AppSettings("cons"))
        Dim MyCommand As SqlCommand
        myConn.Open()
        MyCommand = New SqlCommand("select 1 from quicach", myConn)
        While objStreamReader.Peek() <> -1

            sString = objStreamReader.ReadLine()
            sString = remove_SpeKey(sString)
            If Trim(sString) <> "" Then
                If i > 0 Then
                    ' Data 
                    'On Error Resume Next
                    strDataFields = Split(sString, ",")
                    If strDataFields(0) <> "" And IsNumeric(strDataFields(iIndex)) Then
                        sSql = "update quicach set " + txtData.Text + " =" & strDataFields(iIndex) & ", codecn='" & strCode.ToString & "', ngaycn=getdate() where " + txtMa.Text + "='" & strDataFields(0) & "'"
                        MyCommand.CommandText = sSql
                        MyCommand.ExecuteNonQuery()
                        iCount += 1
                    End If
                End If
                i = i + 1
            End If
        End While
        objStreamReader.Close()
        Label1.Text = iCount.ToString
        sSql = "select count(1) from quicach where convert(varchar(10),ngaycn,103)=convert(varchar(10),getdate(),103) and codecn='" & strCode & "'"
        MyCommand.CommandText = sSql
        Label2.Text = MyCommand.ExecuteScalar
        MyCommand.Dispose()
        myConn.Close()
        Exit Sub
hErr:
        objStreamReader.Close()
        MyCommand.Dispose()
        myConn.Close()
    End Sub

    Private Function remove_SpeKey(ByVal vString As String) As String
        Dim strString As String
        strString = Replace(vString, """", "")
        Return strString
    End Function
End Class
